<#
# 使用事例：
# 在目标目录执行脚本


#>
# 脚本参数定义
param(
    [Parameter(Mandatory=$false)]
    # 使用 [switch] 类型表示布尔开关（无需值）
    [switch]$Test

    # 参数1
)

# # 检查是否以管理员身份运行
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    # 构建重新启动脚本的参数
    $arguments = "-File `"$($MyInvocation.MyCommand.Path)`""
    $arguments += " $args"  # 传递原始参数
    
    # 启动新进程并请求管理员权限
    try {
        Start-Process powershell.exe -Verb RunAs -ArgumentList $arguments -ErrorAction Stop
        exit
    }
    catch {
        # Write-Warning "提权请求被拒绝，脚本退出。"
        Write-Warning "The privilege escalation request is denied and the script exits."
        exit 1
    }
    # 退出当前非管理员进程
    exit
}


# # 以下是需要管理员权限的脚本内容
# # Write-Host "脚本已以管理员权限运行！"
# Write-Host "The script is running with administrator privileges!"

# 你的其他代码..


# 检查是否有某命令
function which ($command) {
    Get-Command -Name $command -ErrorAction SilentlyContinue |
      Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}

# 日志打印
function XWrite-Host ($FLAG,$String) {
    # $NowDateTime = $(Get-Date -f "MM-dd HH:mm:ss")
    $NowDateTime = $(Get-Date -f "HH:mm:ss")
    if ($Test) {# 传递了 --test 参数
        switch($FLAG){
        # switch -Wildcard ( $FLAG ){
            # 'Info' { 
            #     Write-Information "[$FLAG][$NowDateTime] $String ."   
            # }
            { $_ -in "Info", "INFO" } {
                # Write-Information "[$FLAG][$NowDateTime] $String ." 
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor White 
            }
            'Warning' { 
                # Write-Warning -Message "[$FLAG][$NowDateTime] $String ."  
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Yellow  
            }
            { $_ -in "Debug", "DEBUG" } { 
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Blue 
            }
            { $_ -in "Error", "ERROR" } { 
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Red 
                # Write-Error -Message "[$FLAG][$NowDateTime] $String ." 
            }
            default { Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Green }
        }
    } else {
        switch($FLAG){
            { $_ -in "Info", "INFO" }  {
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor White 
            }
            { $_ -in "Error", "ERROR" } { 
                Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Red 
            }
            default { 
                # Write-Host "[$FLAG][$NowDateTime] $String ."  -ForegroundColor Green 
            }
        }
    }
}



# 函数 主入口
function main(){
    XWrite-Host "Debug" "main()"
    $ProjectRoot = Split-Path -Path $PSScriptRoot # 获取上级父目录地址
    # 检查是否已经安装了yazi
    $softwareName = "yazi"
    if ( -not $(which "$softwareName") ) { # 不存在，则下载解压，添加环境变量
        XWrite-Host "INFO" "$softwareName is not exist."
    } else {
        XWrite-Host "Debug" "$softwareName is  exist."
        # 检查是否已经存在配置文件了，有则备份，
        $TargetLinkPath = "${HOME}\AppData\Roaming\yazi"
        if (Test-Path $TargetLinkPath) {
            XWrite-Host "INFO" "$TargetLinkPath is exist."
        } else {
            XWrite-Host "INFO" "$TargetLinkPath is not exist."
            # $ConfigFolderPath = "$ProjectRoot\yazi"
            $ConfigFolderPath = "$PSScriptRoot"
            # $(make-file-link-func  "${HOME}\AppData\Roaming\yazi" "./")
            # $(make-file-link-func  $TargetLinkPath $ConfigFolderPath)
            New-Item -ItemType SymbolicLink -Path "$TargetLinkPath" -Target "$ConfigFolderPath"
        } 
    }
}

# 开始执行
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew() # 创建并启动计时器 (高精度)
XWrite-Host "Debug" "stopwatch = $stopwatch"
if ($Test) {# 检查是否传递了 --test 参数
    # Write-Host "测试模式"
    Write-Host "Test Mode"
    main
} else {
    # Write-Host "普通模式"
    main
}
# 停止计时器并获取结果
$stopwatch.Stop()
$elapsedTime = $stopwatch.Elapsed
XWrite-Host "INFO" "Script execution time`: $($elapsedTime.ToString('hh\:mm\:ss\.fff')) "# 输出格式化时间（例如：00:00:05.123）

# 显示倒计时（进阶用法）
1..5 | ForEach-Object {
    # Write-Host "等待 $_ 秒..." -ForegroundColor Cyan
    Write-Host "Wait $_ seconds ..." -ForegroundColor Cyan
    Start-Sleep -Seconds 1
}

# Write-Host "按任意键继续..." -ForegroundColor Yellow
Write-Host "Press any key to continue..." -ForegroundColor Yellow
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")  # 无回显，捕获所有按键

# Read-Host "按 Enter 继续..."  # 必须按回车键